// composables/useModelInference.js
import { ref } from 'vue'
import moment from 'moment'
import { inferenceApi } from '@/api/inference'

export function useInference() {
  const result = ref(null)
  const loading = ref(false)
  const error = ref(null)

  const runInference = async (params) => {
    loading.value = true
    try {
      const response = await inferenceApi.runInference(params)
      result.value = response.data
      return response.data
    } catch (err) {
      error.value = err.message
      throw err
    } finally {
      loading.value = false
    }
  }
  const runInferenceBatch = async (params) => {
    loading.value = true
    console.log('inference batch', params)
    try {
      const response = await inferenceApi.runInferenceBatch(params)
      result.value = response.data
      console.log('inference batch result', result.value)
      return response.data
    } catch (err) {
      error.value = err.message
      throw err
    } finally {
      loading.value = false
    }
  }

  const runUpdateSamples = async (params) => {
    loading.value = true
    try {
      const response = await inferenceApi.updateSamplesAPI(params)
      result.value = response.data
      return response.data
    } catch (err) {
      error.value = err.message
      throw err
    } finally {
      loading.value = false
    }
  }

  const runCouldEvolution = async (params) => {
    const finalParams = {
      ...params,
      dataStartTime: moment(params.dataStartTime).format('YYYYMMDD'),
      dataEndTime: moment(params.dataEndTime).format('YYYYMMDD')
    }
    loading.value = true
    try {
      const response = await inferenceApi.couldEvolutionApi(finalParams)
      result.value = response.data
      return response.data
    } catch (err) {
      error.value = err.message
      throw err
    } finally {
      loading.value = false
    }
  }

  const runCollaborateEvolution = async (params) => {
    const finalParams = {
      ...params,
      dataStartTime: moment(params.dataStartTime).format('YYYYMMDD'),
      dataEndTime: moment(params.dataEndTime).format('YYYYMMDD')
    }
    loading.value = true
    try {
      const response = await inferenceApi.collaborateEvolutionApi(finalParams)
      result.value = response.data
      return response.data
    } catch (err) {
      error.value = err.message
      throw err
    } finally {
      loading.value = false
    }
  }

  const getTaskProgress = async (taskID) => {
    loading.value = true
    try {
      const response = await inferenceApi.getTaskProgress(taskID)
      result.value = response.data
      return response.data
    } catch (err) {
      error.value = err.message
      throw err
    } finally {
      loading.value = false
    }
  }

  const setStrategy = async (params) => {
    loading.value = true
    try {
      const response = await inferenceApi.setStrategy(params)
      result.value = response.data
      return response.data
    } catch (err) {
      error.value = err.message
      throw err
    } finally {
      loading.value = false
    }
  }

  const getGPUStatus = async (gputype) => {
    loading.value = true
    try {
      const response = await inferenceApi.getGPUStatus(gputype)
      result.value = response.data
      return response.data
    } catch (err) {
      error.value = err.message
      throw err
    } finally {
      loading.value = false
    }
  }

  const getFileByPath = async (filePath) => {
    loading.value = true
    try {
      const response = await inferenceApi.getFileByPath(filePath)
      result.value = response.data
      return response.data
    } catch (err) {
      error.value = err.message
      throw err
    } finally {
      loading.value = false
    }
  }

  return {
    result,
    loading,
    error,
    runInference,
    runInferenceBatch,
    runUpdateSamples,
    runCouldEvolution,
    runCollaborateEvolution,
    getTaskProgress,
    setStrategy,
    getGPUStatus,
    getFileByPath
  }
}
